package com.dameng.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dameng.system.entity.SystemMenu;
import com.dameng.system.entity.SystemRole;
import com.dameng.system.entity.SystemRoleMenu;
import com.dameng.system.mapper.SystemMenuMapper;
import com.dameng.system.mapper.SystemRoleMapper;
import com.dameng.system.mapper.SystemRoleMenuMapper;
import com.dameng.system.service.PermissionService;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class PermissionServiceImpl implements PermissionService {

    @Resource
    SystemMenuMapper systemMenuMapper;

    @Resource
    SystemRoleMenuMapper systemRoleMenuMapper;

    @Resource
    SystemRoleMapper systemRoleMapper;

    private Map<String, Collection<ConfigAttribute>> permissionMap = null;

    @PostConstruct
    public void initPermissions() {
        permissionMap = new HashMap<>();

        List<SystemMenu> systemMenus = systemMenuMapper.selectList(
                new QueryWrapper<SystemMenu>().lambda().ne(SystemMenu::getUrlRequest, "/")
                        .eq(SystemMenu::getStatus, true)
        );
        if (systemMenus != null && !systemMenus.isEmpty()) {
            for (SystemMenu systemMenu : systemMenus) {
                Collection<ConfigAttribute> collection = new ArrayList<>();
                List<SystemRoleMenu> systemRoleMenus = systemRoleMenuMapper.selectList(
                        new QueryWrapper<SystemRoleMenu>().lambda().eq(SystemRoleMenu::getMenuId, systemMenu.getMenuId())
                );
                if (systemRoleMenus != null && !systemRoleMenus.isEmpty()) {
                    List<String> roleIds = systemRoleMenus.stream().map(SystemRoleMenu::getRoleId).collect(Collectors.toList());
                    List<SystemRole> systemRoles = systemRoleMapper.selectList(
                            new QueryWrapper<SystemRole>().lambda().in(SystemRole::getRoleId, roleIds)
                    );
                    for (SystemRole systemRole : systemRoles) {
                        ConfigAttribute config = new SecurityConfig(systemRole.getName());
                        collection.add(config);
                    }
                }
                String url = systemMenu.getUrlRequest();
                String post = url.split("/")[url.split("/").length-1];
                String permission = url.substring(0, url.length() - post.length()) + "*";
                permissionMap.put(permission, collection);
            }
        }
    }

    @Override
    public Map<String, Collection<ConfigAttribute>> getPermissionMap() {
        if (permissionMap.size() == 0) {
            initPermissions();
        }
        return permissionMap;
    }

    @Override
    public void cleanPermissionMap() {
        permissionMap.clear();
    }
}
